// pages/movieDetail/movieDetail.js
var MCOLOR = require('../../../libs/mcolor')

Page({
  data: {
    loading: true,
    movieInfo: {},
    movieBg: ''
  },
  onLoad: function (options) {
    this.setData({
      onling: getApp().globalData.onling,
    })
    if (this.data.onling) return
    this.getMovieDetail(options)
  },
  onReady() {
    var _this = this
    var path = 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2577327922.jpg'
    var Path = '../../../images/ic_share.png'
    var ctx = wx.createCanvasContext('myCanvas')
    const mdata = new Uint8ClampedArray()
    // console.log("data", mdata)
    // this._putImageOnCanvas(mdata)
  },
  //获取网络图片的信息
  _getImageInfo(image_path) {
    var _this = this
    wx.getImageInfo({
      src: image_path, //服务器返回的图片地址
      success: function (res) {
        //res.path是网络图片的本地地址
        _this._drawImageOnCanvas(res.path)
      },
      fail: function (res) {
        //失败回调
      }
    });
  },
  //在画布绘制图片
  _drawImageOnCanvas(path) {
    var _this = this
    var ctx = wx.createCanvasContext('myCanvas')

    ctx.drawImage(path, 0, 0, 160, 223)
    ctx.draw(false, function (e) {
      console.log('draw callback', e)
      _this._getImageDataFromCanvas()
    })
  },
  //在画布绘制图片信息
  _putImageOnCanvas(imageData) {
    wx.canvasPutImageData({
      canvasId: 'myCanvas',
      x: 0,
      y: 0,
      width: 2,
      height: 5,
      data: imageData,
      success(res) {
        console.log("success", res)
      },
      fail(res) {
        console.error("fail", res)
      }
    })
  },
  //获取画布上的图片信息
  _getImageDataFromCanvas() {
    var _this = this
    wx.canvasGetImageData({
      canvasId: 'myCanvas',
      x: 0,
      y: 0,
      width: 160,
      height: 223,
      success(res) {
        var imageData = res.data;
        console.log("imageData", imageData)
        // _this._putImageOnCanvas(imageData.data)
        var img = {
          width: 160,
          height: 223
        }
        var mColor = MCOLOR.getImageColor(img, imageData)
        console.log("mColor", mColor)
        _this.setData({
          movieBg: mColor
        })
      }
    })
  },
  onShareAppMessage() {

  },
  getMovieDetail(options) {
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    console.log(options)
    wx.cloud.callFunction({
      name: 'movie',
      data: {
        action: 'getMovieDetail',
        movie_id: options.subject
      },
      success: res => {
        wx.hideLoading()
        console.log("getMovieDetail", res)
        var movieObj = res.result.data;
        // movieObj.summary = movieObj.summary.replace(/。\s/g, '。\n&nbsp;&nbsp;&nbsp;&nbsp;')
        // movieObj.summary = '&nbsp;&nbsp;&nbsp;&nbsp;' + movieObj.summary
        //获取图片信息
        // _this._getImageInfo(movieObj.images.medium)
        _this.setData({
          loading: false,
          movieInfo: movieObj
        })
      },
      fail: err => {
        console.error(err)
        wx.hideLoading()
      }
    })
  },
  // 预告列表点击
  _onYuGaoClick: function (event) {
    var alt = event.target.dataset.alt
    let photos = this.data.movieInfo.photos
    console.log(alt)
    wx.previewImage({
      current: photos[alt],
      urls: photos,
    })
  },
})